|
1
|
|
|
import { call, put, takeLatest, select } from 'redux-saga/effects' |
|
2
|
|
|
import { fetchPostFromSlug, savingPost, createPost } from 'post/api/fetch' |
|
3
|
|
|
import { |
|
4
|
|
|
FETCH_POST, |
|
5
|
|
|
SAVE_EDITED_CONTENT, |
|
6
|
|
|
CREATE_NEW_POST, |
|
7
|
|
|
OPEN_TOAST |
|
8
|
|
|
} from 'core/state/actionType' |
|
9
|
|
|
import { toSuccess, toError, toRequest } from 'core/state/utils' |
|
10
|
|
|
import { getSavingContent } from 'post/state/selectors' |
|
11
|
|
|
|
|
12
|
|
|
function* fetchPost({ payload: slug }) { |
|
13
|
|
|
try { |
|
14
|
|
|
const post = yield call(fetchPostFromSlug, slug) |
|
|
|
|
|
|
15
|
|
|
yield put({ type: toSuccess(FETCH_POST), payload: post }) |
|
16
|
|
|
} catch (e) { |
|
17
|
|
|
yield put({ type: toError(FETCH_POST), error: e }) |
|
18
|
|
|
} |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
function* savePost() { |
|
22
|
|
|
const state = yield select() |
|
23
|
|
|
try { |
|
24
|
|
|
yield call(savingPost, getSavingContent(state)) |
|
25
|
|
|
yield put({ type: toSuccess(SAVE_EDITED_CONTENT), payload: '' }) |
|
26
|
|
|
yield put({ |
|
27
|
|
|
type: OPEN_TOAST, |
|
28
|
|
|
payload: { msg: 'Successfully saving post', success: true } |
|
29
|
|
|
}) |
|
30
|
|
|
} catch (e) { |
|
31
|
|
|
yield put({ type: toError(SAVE_EDITED_CONTENT), error: e }) |
|
32
|
|
|
yield put({ |
|
33
|
|
|
type: OPEN_TOAST, |
|
34
|
|
|
payload: { msg: 'Error while saving post', success: false } |
|
35
|
|
|
}) |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
function* createPostAction() { |
|
40
|
|
|
const state = yield select() |
|
41
|
|
|
try { |
|
42
|
|
|
yield call(createPost, getSavingContent(state)) |
|
43
|
|
|
yield put({ type: toSuccess(CREATE_NEW_POST), payload: '' }) |
|
44
|
|
|
yield put({ |
|
45
|
|
|
type: OPEN_TOAST, |
|
46
|
|
|
payload: { msg: 'Successfully creating post', success: true } |
|
47
|
|
|
}) |
|
48
|
|
|
} catch (e) { |
|
49
|
|
|
yield put({ type: toError(CREATE_NEW_POST), error: e }) |
|
50
|
|
|
yield put({ |
|
51
|
|
|
type: OPEN_TOAST, |
|
52
|
|
|
payload: { msg: 'Error while creating post', success: false } |
|
53
|
|
|
}) |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
function* postSaga() { |
|
58
|
|
|
yield takeLatest(toRequest(FETCH_POST), fetchPost) |
|
59
|
|
|
yield takeLatest(toRequest(SAVE_EDITED_CONTENT), savePost) |
|
60
|
|
|
yield takeLatest(toRequest(CREATE_NEW_POST), createPostAction) |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
export default postSaga |
|
64
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.